/*
 * Copyright (C) 2008 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.ekuma.android.ui.scan;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.Collection;

import cn.ekuma.client.android.scantool.R;
import cn.ekuma.epos.android.Config;

/**
 * The main settings activity.
 *
 * @author dswitkin@google.com (Daniel Switkin)
 */
public final class PreferencesActivity extends PreferenceActivity
    implements OnSharedPreferenceChangeListener {


  
  private CheckBoxPreference decode1D;
  private CheckBoxPreference decodeQR;
  private CheckBoxPreference decodeDataMatrix;
  private ListPreference lp;
  private CheckBoxPreference lotNumber;
  private EditTextPreference numberPattern;
  private EditTextPreference customProductSearch;
    private CheckBoxPreference dbLocalModel;


    protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    addPreferencesFromResource(R.xml.preferences);

    /*PreferenceScreen preferences = getPreferenceScreen();
    preferences.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    decode1D = (CheckBoxPreference) preferences.findPreference(KEY_DECODE_1D);
    decodeQR = (CheckBoxPreference) preferences.findPreference(KEY_DECODE_QR);
    decodeDataMatrix = (CheckBoxPreference) preferences.findPreference(KEY_DECODE_DATA_MATRIX);
    
    disableLastCheckedPref();*/
   
    //getPreferenceManager().setSharedPreferencesName(Intents.PREFS_NAME);
    //getPreferenceManager().createPreferenceScreen();
    PreferenceScreen preferences = getPreferenceScreen();
    preferences.getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    decode1D = (CheckBoxPreference) preferences.findPreference(Config.KEY_DECODE_1D);
    decodeQR = (CheckBoxPreference) preferences.findPreference(Config.KEY_DECODE_QR);
    decodeDataMatrix = (CheckBoxPreference) preferences.findPreference(Config.KEY_DECODE_DATA_MATRIX);
      //初始化这个ListPreference对象
    lp=(ListPreference)preferences.findPreference(Config.KEY_EXPORT_TYPE);
    lotNumber= (CheckBoxPreference) preferences.findPreference(Config.KEY_LOT_NUMBER);
    numberPattern=(EditTextPreference)preferences.findPreference(Config.KEY_LOT_NUMBER_PATTERN);

   customProductSearch= (EditTextPreference)preferences.findPreference(Config.KEY_CUSTOM_PRODUCT_SEARCH);
   dbLocalModel = (CheckBoxPreference) preferences.findPreference(Config.KEY_DB_LOCALMODEL);
   disableLastCheckedPref();
   getActionBar().setDisplayHomeAsUpEnabled(true);
  }

 
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
      disableLastCheckedPref();
  }

  private void disableLastCheckedPref() {
    Collection<CheckBoxPreference> checked = new ArrayList<CheckBoxPreference>(3);
    if (decode1D.isChecked()) {
      checked.add(decode1D);
    }
    if (decodeQR.isChecked()) {
      checked.add(decodeQR);
    }
    if (decodeDataMatrix.isChecked()) {
      checked.add(decodeDataMatrix);
    }
    boolean disable = checked.size() < 2;
    CheckBoxPreference[] checkBoxPreferences = {decode1D, decodeQR, decodeDataMatrix};
    for (CheckBoxPreference pref : checkBoxPreferences) {
      pref.setEnabled(!(disable && checked.contains(pref)));
    }
    lp.setSummary(lp.getEntry());
    numberPattern.setEnabled(lotNumber.isChecked());
    customProductSearch.setEnabled(!lotNumber.isChecked());
    dbLocalModel.setEnabled(!lotNumber.isChecked());
  }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
